/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.agent.tools;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

/**
 *
 * @author root
 */
public class FtpTools1 {

    public static int ftpNumber = 0;
    private static Logger logger = Logger.getLogger(FtpTools1.class);
    public static long fileSize = 0l;
    public static long finlaFileSize = 0l;

    /**
     * Description: 从FTP服务器下载文件
     *
     * @Version1.0 2 17, 2016 5:32:36 PM by 张云（cuihongbao@d-heaven.com）创建
     * @param url FTP服务器hostname
     * @param port FTP服务器端口
     * @param username FTP登录账号
     * @param password FTP登录密码
     * @param remotePath FTP服务器上的相对路径
     * @param fileName 要下载的文件名
     * @param localPath 下载后保存到本地的路径
     * @return
     */
    public static synchronized boolean downFile(String url, int port, String username, String password,
            String remotePath, String localPath, String filetype) {
        logger.info("FtpTools downFile start");
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login(username, password);//登录
            ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
            reply = ftp.getReplyCode();
            ftpNumber = reply;
            logger.info("reply: " + reply);
            if (!FTPReply.isPositiveCompletion(reply)) {
                logger.info("if (!FTPReply.isPositiveCompletion(reply)) {");
                ftp.disconnect();
                logger.info(url + " ftp login err!username=" + username + ",password=" + password);
                return success;
            }

            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
            ftp.enterLocalPassiveMode();//在linux上，由于安全限制，可能某些端口没有开启，所以就出现阻塞.开通一个端口来传输数据
            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录

            boolean activeFlag = false;
            try {
                FTPFile[] fs0 = ftp.listFiles();
                logger.info(url + " ftp list size is " + fs0.length);
            } catch (Exception e) {
                logger.error("ftp.listFiles error. " + e.getMessage(), e);
                activeFlag = true;
            } finally {
                if (activeFlag) {
                    //设置为主动
                    ftp.enterLocalActiveMode();
                    logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
                }
            }

            FTPFile[] fs = ftp.listFiles();
            logger.debug(url + " ftp list size is " + fs.length);
            List<FTPFile> finalList = new ArrayList<FTPFile>();
            for (FTPFile ff : fs) {
                if (PatternTools.matchStr(filetype, ff.getName())) {
                    finalList.add(ff);
                    logger.info(url + " finalList contains:" + ff.getName());
                }
            }

            finlaFileSize = finalList.size();
            for (FTPFile ff : finalList) {
                try {
                    File rootDir = new File(localPath);
                    if (!rootDir.exists()) {
                        rootDir.mkdirs();
                    }
                    File localFile = new File(localPath + "/" + ff.getName());
                    OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();

                    logger.info(url + " " + ff.getName() + " download sucess!");
                } catch (Exception ex) {
                    logger.error(ff.getName() + " download err!" + ex.getMessage(), ex);
                }
                success = true;
            }
            ftp.logout();

        } catch (Exception e) {
            logger.error(url + " ftp download err " + e.getMessage(), e);
            ftpNumber = 530;
            return success;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        logger.info(url + " ftp download finish!");
        return success;
    }

    //计数下载1000个json文件
    public static synchronized List<String> downFile(String url, int port, String username, String password,
            String remotePath, String localPath, String filetype, String namespace) {
        logger.info("FtpTools downFile start");
        logger.info("url:" + url + ", port: " + port + ",username: " + username + ",password: " + password + ",remotepath: " + remotePath + ",localpath: " + localPath);
        List<String> jsonList = new ArrayList<String>();//返回的jsonList
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login(username, password);//登录
            ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
            reply = ftp.getReplyCode();
            ftpNumber = reply;
            logger.info("reply: " + reply);
            if (!FTPReply.isPositiveCompletion(reply)) {
                logger.info("if (!FTPReply.isPositiveCompletion(reply)) {");
                ftp.disconnect();
                logger.info(url + " ftp login err!username=" + username + ",password=" + password);
                return jsonList;
            }

            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
            ftp.enterLocalPassiveMode();//在linux上，由于安全限制，可能某些端口没有开启，所以就出现阻塞.开通一个端口来传输数据
            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            boolean changeWorkingDirectory = ftp.changeWorkingDirectory(remotePath); //转移到FTP服务器目录
            logger.info("changeWorkingDirectory " + remotePath + " return " + changeWorkingDirectory);

            boolean activeFlag = false;
            try {
                FTPFile[] fs0 = ftp.listFiles();
                logger.info(url + " ftp list size is " + fs0.length);
            } catch (Exception e) {
                logger.error("ftp.listFiles error. " + e.getMessage(), e);
                activeFlag = true;
            } finally {
                if (activeFlag) {
                    //设置为主动
                    ftp.enterLocalActiveMode();
                    logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
                }
            }

            FTPFile[] fs = ftp.listFiles();
            logger.debug(url + " ftp list size is " + fs.length);
            for (int m = 0; m < fs.length; m++) {
                logger.debug(url + " ftp list[" + m + "]=" + fs[m].getName());
            }
            List<FTPFile> txtNameList = new ArrayList<FTPFile>();
            List<FTPFile> okList = new ArrayList<FTPFile>();
            List<FTPFile> dwdList = new ArrayList<FTPFile>();
            List<FTPFile> finalList = new ArrayList<FTPFile>();
            for (FTPFile ff : fs) {
                if (PatternTools.matchStr(filetype, ff.getName())) {
                    if (ff.getName().endsWith(".dwd")) {
                        dwdList.add(ff);
                    } else if (ff.getName().endsWith(".ok")) {
                        okList.add(ff);
                    } else {
                        txtNameList.add(ff);
                    }
                }
            }
            fileSize = okList.size();
            for (int i = 0; i < txtNameList.size(); i++) {
                if (PatternTools.matchStr(filetype, txtNameList.get(i).getName())) {
                    String tmpokname = txtNameList.get(i).getName() + ".ok";
                    for (int j = 0; j < okList.size(); j++) {
                        if (tmpokname.equals(okList.get(j).getName())) {
                            boolean flag = false;
                            for (int k = 0; k < dwdList.size(); k++) {
                                String tmpdwdname = txtNameList.get(i).getName() + "." + namespace + ".dwd";
                                if (tmpdwdname.equals(dwdList.get(k).getName())) {
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag) {
                                finalList.add(txtNameList.get(i));
                            }
                        }
                    }
                }
            }
            for (FTPFile ff : finalList) {
                logger.debug(url + " finalList contains:" + ff.getName());
            }
            finlaFileSize = finalList.size();

            int count = 0;
            PropsFiles ps = new PropsFiles();
            String str = ps.getValue("jsonCount");
            int max = Integer.parseInt(str);
            for (FTPFile ff : finalList) {
                if (count < max) {
                    try {
                        File rootDir = new File(localPath);
                        if (!rootDir.exists()) {
                            rootDir.mkdirs();
                        }
                        File localFile = new File(localPath + "/" + ff.getName());
                        OutputStream is = new FileOutputStream(localFile);
                        ftp.retrieveFile(ff.getName(), is);
                        is.close();
                        jsonList.add(ff.getName());
                        logger.info(url + " " + ff.getName() + " download sucess!");
                        count++;
                    } catch (Exception ex) {
                        logger.error(ff.getName() + " download err!" + ex.getMessage(), ex);
                        return jsonList;
                    }
                } else {
                    logger.info("download json files sum 1000!");
                    break;
                }
            }
            ftp.logout();
        } catch (Exception e) {
            logger.error(url + " ftp download err " + e.getMessage(), e);
            ftpNumber = 530;
            return jsonList;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        logger.info(url + " ftp download finish!");
        return jsonList;
    }

    //计数下载100个json文件
    public static synchronized boolean downJsonFile(String url, int port, String username, String password,
            String remotePath, String localPath, String[] filename) {
        logger.info("FtpTools downJsonFile start");
        logger.info("url:" + url + ", port: " + port + ",username: " + username + ",password: " + password + ",remotepath: " + remotePath + ",localpath: " + localPath);
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login(username, password);//登录
            ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
            reply = ftp.getReplyCode();
            logger.info("reply: " + reply);
            if (!FTPReply.isPositiveCompletion(reply)) {
                logger.info("if (!FTPReply.isPositiveCompletion(reply)) {");
                ftp.disconnect();
                logger.info(url + " ftp login err!username=" + username + ",password=" + password);
                return false;
            }

            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
            ftp.enterLocalPassiveMode();//在linux上，由于安全限制，可能某些端口没有开启，所以就出现阻塞.开通一个端口来传输数据
            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            boolean changeWorkingDirectory = ftp.changeWorkingDirectory(remotePath); //转移到FTP服务器目录
            logger.info("changeWorkingDirectory " + remotePath + " return " + changeWorkingDirectory);

            boolean activeFlag = false;
            try {
                FTPFile[] fs0 = ftp.listFiles();
                logger.info(url + " ftp list size is " + fs0.length);
            } catch (Exception e) {
                logger.error("ftp.listFiles error. " + e.getMessage(), e);
                activeFlag = true;
            } finally {
                if (activeFlag) {
                    //设置为主动
                    ftp.enterLocalActiveMode();
                    logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
                }
            }

            FTPFile[] fs = ftp.listFiles();
            logger.info(url + " ftp list size is " + fs.length);
            for (String ff : filename) {
                try {
                    File rootDir = new File(localPath);
                    if (!rootDir.exists()) {
                        rootDir.mkdirs();
                    }
                    File localFile = new File(localPath + "/" + ff);
                    OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff, is);
                    is.close();
                    logger.info(url + " " + ff + " download sucess!");
                } catch (Exception ex) {
                    logger.error(ff + " download err!" + ex.getMessage(), ex);
                }
            }
            ftp.logout();
        } catch (Exception e) {
            logger.error(url + " ftp download err " + e.getMessage(), e);
            return false;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ex) {
                    logger.error(ex.getMessage(), ex);
                }
            }
        }
        logger.info(url + " ftp download finish!");
        return true;
    }

    //ftp上传dwd文件
    public static synchronized boolean dwdFile(String url, int port, String username, String password, String remotePath, String fileName, String namespace) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login(username, password);//登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                logger.info(url + "ftp login err!username=" + username + ",password=" + password);
                return success;
            }

            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
            ftp.enterLocalPassiveMode();//在linux上，由于安全限制，可能某些端口没有开启，所以就出现阻塞.开通一个端口来传输数据
            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录

            boolean activeFlag = false;
            try {
                FTPFile[] fs0 = ftp.listFiles();
                logger.info(url + " ftp list size is " + fs0.length);
            } catch (Exception e) {
                logger.error("ftp.listFiles error. " + e.getMessage(), e);
                activeFlag = true;
            } finally {
                if (activeFlag) {
                    //设置为主动
                    ftp.enterLocalActiveMode();
                    logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
                }
            }

            try {
                logger.info("upload dwd file");
                //upload dwd file
                InputStream dwd = new ByteArrayInputStream("ok".getBytes("utf-8"));
                ftp.storeFile(fileName + "." + namespace + ".dwd", dwd);
                dwd.close();

                success = true;
            } catch (Exception ex) {
                logger.error(remotePath + " upload err!" + ex.getMessage(), ex);
                return success;
            }
            ftp.logout();
        } catch (Exception e) {
            logger.error(url + " ftp upload err " + e.getMessage(), e);
            return success;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }

    //ftp上传文件并打.ok
    public static synchronized boolean upload(String url, int port, String username, String password, String localPath, String remotePath, String fileName) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login(username, password);//登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                logger.info(url + "ftp login err!username=" + username + ",password=" + password);
                return success;
            }
            ftp.setControlEncoding("UTF-8");

            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
            ftp.enterLocalPassiveMode();//在linux上，由于安全限制，可能某些端口没有开启，所以就出现阻塞.开通一个端口来传输数据
            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.changeWorkingDirectory(remotePath); //转移到FTP服务器目录

            boolean activeFlag = false;
            try {
                FTPFile[] fs0 = ftp.listFiles();
                logger.info(url + " ftp list size is " + fs0.length);
            } catch (Exception e) {
                logger.error("ftp.listFiles error. " + e.getMessage(), e);
                activeFlag = true;
            } finally {
                if (activeFlag) {
                    //设置为主动
                    ftp.enterLocalActiveMode();
                    logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
                }
            }

            try {
                //upload dwd file
                FileInputStream in = new FileInputStream(new File(localPath));
                ftp.storeFile(fileName, in);
                in.close();

                InputStream ok = new ByteArrayInputStream("ok".getBytes("utf-8"));
                ftp.storeFile(fileName + ".ok", ok);
                ok.close();

                success = true;
            } catch (Exception ex) {
                logger.error(localPath + " upload err!" + ex.getMessage(), ex);
                return success;
            }
            ftp.logout();
        } catch (Exception e) {
            logger.error(url + " ftp upload err " + e.getMessage(), e);
            return success;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }

    //ftp上传.ok文件
    public static synchronized boolean okFile(String url, int port, String username, String password, String remotePath, String fileName) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login(username, password);//登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                logger.info(url + "ftp login err!username=" + username + ",password=" + password);
                return success;
            }

            ftp.setControlEncoding("UTF-8");

            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
            ftp.enterLocalPassiveMode();//在linux上，由于安全限制，可能某些端口没有开启，所以就出现阻塞.开通一个端口来传输数据
            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.changeWorkingDirectory(remotePath); //转移到FTP服务器目录

            boolean activeFlag = false;
            try {
                FTPFile[] fs0 = ftp.listFiles();
                logger.info(url + " ftp list size is " + fs0.length);
            } catch (Exception e) {
                logger.error("ftp.listFiles error. " + e.getMessage(), e);
                activeFlag = true;
            } finally {
                if (activeFlag) {
                    //设置为主动
                    ftp.enterLocalActiveMode();
                    logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
                }
            }

            try {
                InputStream ok = new ByteArrayInputStream("ok".getBytes("utf-8"));
                ftp.storeFile(fileName + ".ok", ok);
                ok.close();

                success = true;
            } catch (Exception ex) {
                logger.error("upload err!" + ex.getMessage(), ex);
                return success;
            }
            ftp.logout();
        } catch (Exception e) {
            logger.error(url + " ftp upload err " + e.getMessage(), e);
            return success;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }

    //ftp上传文件,并创建多级目录
    public static synchronized boolean upload(String url, int port, String username, String password, String localPath, String remotePath, String subdirectory, String fileName) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login(username, password);//登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                logger.info(url + "ftp login err!username=" + username + ",password=" + password);
                return success;
            }
            ftp.setControlEncoding("UTF-8");

            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
            ftp.enterLocalPassiveMode();//在linux上，由于安全限制，可能某些端口没有开启，所以就出现阻塞.开通一个端口来传输数据
            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            //创建目录
            boolean changeWorkingDirectory = ftp.changeWorkingDirectory(remotePath);
            if (!changeWorkingDirectory) {
                ftp.makeDirectory(remotePath);
                changeWorkingDirectory = ftp.changeWorkingDirectory(remotePath);
            }
            logger.info("remotePath：" + remotePath + "-------------" + changeWorkingDirectory);

            boolean activeFlag = false;
            try {
                FTPFile[] fs0 = ftp.listFiles();
                logger.info(url + " ftp list size is " + fs0.length);
            } catch (Exception e) {
                logger.error("ftp.listFiles error. " + e.getMessage(), e);
                activeFlag = true;
            } finally {
                if (activeFlag) {
                    //设置为主动
                    ftp.enterLocalActiveMode();
                    logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
                }
            }

            boolean changeflag = false;
            subdirectory = subdirectory.replaceAll("//", "/");
            if (subdirectory.startsWith("/")) {
                subdirectory = subdirectory.substring(subdirectory.indexOf("/") + 1, subdirectory.length());
            }
            if (subdirectory.endsWith("/")) {
                subdirectory = subdirectory.substring(0, subdirectory.lastIndexOf("/"));
            }
            String[] str = subdirectory.split("/");
            for (int i = 0; i < str.length; i++) {
                String path = str[i];
                boolean makeDirectory = ftp.makeDirectory(path);
                changeflag = ftp.changeWorkingDirectory(path);
                logger.info("path=" + path + " ,创建目录：" + makeDirectory + " ,切换目录： " + changeflag);
            }

            if (!changeflag) {
                return success;
            } else {
                try {
                    //upload dwd file
                    FileInputStream in = new FileInputStream(new File(localPath));
                    ftp.storeFile(fileName, in);
                    in.close();

                    success = true;
                } catch (Exception ex) {
                    logger.error(localPath + " upload err!" + ex.getMessage(), ex);
                    return success;
                }
            }
            ftp.logout();
        } catch (Exception e) {
            logger.error(url + " ftp upload err " + e.getMessage(), e);
            return success;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }

    /**
     * Description: 向FTP服务器上传文件
     *
     * @Version1.0 Jul 27, 2008 4:31:09 PM by 崔红保（cuihongbao@d-heaven.com）创建
     * @param url FTP服务器hostname
     * @param port FTP服务器端口
     * @param username FTP登录账号
     * @param password FTP登录密码
     * @param path FTP服务器保存目录
     * @param filename 上传到FTP服务器上的文件名
     * @param input 输入流
     * @return 成功返回true，否则返回false
     */
    public static synchronized boolean uploadFile(String url, int port, String username, String password, String path, String filename, InputStream input) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);//连接FTP服务器
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器 
            ftp.login(username, password);//登录 
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return success;
            }

            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
            ftp.enterLocalPassiveMode();
            logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.changeWorkingDirectory(path);

            boolean activeFlag = false;
            try {
                FTPFile[] fs0 = ftp.listFiles();
                logger.info(url + " ftp list size is " + fs0.length);
            } catch (Exception e) {
                logger.error("ftp.listFiles error. " + e.getMessage(), e);
                activeFlag = true;
            } finally {
                if (activeFlag) {
                    //设置为主动
                    ftp.enterLocalActiveMode();
                    logger.info("current getDataConnectionMode: " + ftp.getDataConnectionMode());
                }
            }

            ftp.storeFile(filename, input);
            input.close();
            ftp.logout();
            success = true;
        } catch (Exception e) {
            logger.error(url + " ftp upload err " + e.getMessage(), e);
            return success;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }
}
